package com.awifi.com.thread.communication;

/**
 * @description: 蛋糕消费者
 * @author: marin
 * @time: 2020/6/29 16:19
 */
public class Consumer extends Thread {

        private Cake cake;

        public Consumer(String name,Cake cake) {
            super(name);
            this.cake = cake;
        }

        @Override
        public void run() {
            while (true){
                synchronized (cake){
                    if (!cake.isFlag()) {
                        try {
                            cake.wait();
                        }catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("开始消费蛋糕");
                    cake.setFlag(false);
                    cake.notify();
                }
            }
        }
}
